﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ServerSetting
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            InitData();
            InitEvent();
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        public virtual void InitData()
        {
            tvMain.HideSelection = false;
            tvMain.DrawMode = TreeViewDrawMode.OwnerDrawText;
            tvMain.DrawNode += DrawNode;
        }

        /// <summary>
        /// 初始化事件
        /// </summary>
        public virtual void InitEvent()
        {
            tvMain.NodeMouseClick += MainTVNodeMouseClick;
            tvMain.ItemDrag += MainTvNodeDrag;
            tvMain.DragEnter += MainTvNodeDragEnter;
            tvMain.DragOver += MainTvNodeDragOver;
            tvMain.DragDrop += MainTvNodeDragDrop;
            btnAdd.Click += AddNode;
            btnRemove.Click += RemoveNode;
            btnSave.Click += SaveView;
        }

        /// <summary>
        /// 绘制常亮的节点（失焦时不会消失）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            if ((e.State & TreeNodeStates.Selected) != 0)
            {
                e.Graphics.FillRectangle(Brushes.DarkBlue, e.Node.Bounds);

                Font nodeFont = e.Node.NodeFont;
                if (nodeFont == null) nodeFont = ((TreeView)sender).Font;
                e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, e.Bounds);
            }
            else
            {
                e.DrawDefault = true;
            }
        }

        public virtual void MainTvNodeDrag(object sender, ItemDragEventArgs e)
        {
            Console.WriteLine("MainTvNodeDrag");
            TreeNode dragNode = e.Item as TreeNode;
            TreeView tv = sender as TreeView;
            tv.SelectedNode = dragNode;
            //DoDragDrop(dragNode, DragDropEffects.Move);
            Console.WriteLine(dragNode.Text);
        }

        private void MainTvNodeDragEnter(object sender, DragEventArgs e)
        {
            Console.WriteLine("MainTvNodeDragEnter");
            e.Effect = e.AllowedEffect;
        }
        private void MainTvNodeDragOver(object sender, DragEventArgs e)
        {
            Console.WriteLine("MainTvNodeDragOver");
            TreeView tv = sender as TreeView;
            tv.SelectedNode = tv.GetNodeAt(tv.PointToClient(new Point(e.X, e.Y)));
        }
        private void MainTvNodeDragDrop(object sender, DragEventArgs e)
        {
            Console.WriteLine("MainTvNodeDragDrop");
            TreeView tv = sender as TreeView;
            //取得被拖拽的节点
            TreeNode dragNode = e.Data.GetData(typeof(TreeNode)) as TreeNode;
            if (dragNode.Equals(tv.SelectedNode))
                return;

            if (e.Effect == DragDropEffects.Move)
            {
                if (tv.SelectedNode == null)
                {
                    tv.Nodes.Add(dragNode.Clone() as TreeNode);
                    dragNode.Remove();
                    return;
                }
                dragNode.Remove();
                tv.SelectedNode.Nodes.Add(dragNode);
            }
            dragNode.Expand();
        }

        /// <summary>
        /// 节点点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void MainTVNodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            txtNodeName.Text = e.Node.Text;
        }

        /// <summary>
        /// 新增节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void AddNode(object sender, EventArgs e)
        {
            InputDialogForm input = new InputDialogForm("Node Add","Input new node name:");
            DialogResult r = input.ShowDialog();
            if (r == DialogResult.OK && !string.IsNullOrEmpty(input.Input))
            {
                TreeNode newNode = null;
                if (tvMain.SelectedNode != null)
                {
                    newNode= tvMain.SelectedNode.Nodes.Add(input.Input);
                    tvMain.SelectedNode.Expand();
                }
                else
                {
                    newNode=tvMain.Nodes.Add(input.Input);
                }

                tvMain.SelectedNode = newNode;
            }
        }

        /// <summary>
        /// 移除节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void RemoveNode(object sender, EventArgs e)
        {
            tvMain.SelectedNode.Remove();
        }

        /// <summary>
        /// 存储当前树结构
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void SaveView(object sender, EventArgs e)
        {
        }
    }
}
